package com.whpost.service.service.t;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whpost.service.dao.t.TDrpcDao;
import com.whpost.service.dao.t.TFilesMapper;
import com.whpost.service.dao.t.TRoleDao;
import com.whpost.service.dao.t.TXzjgDao;
import com.whpost.service.entity.t.*;
import com.whpost.service.entity.tools.ListInfo;
import com.whpost.service.entity.tools.PageInfo;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.BaseServices;
import com.whpost.service.tools.ToolBean;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by IntelliJ IDEA.
 *
 * @Author: Yin Zhuoyi
 * @Date: 2021/12/15 11:16
 */
@Service
public class TDrpcServices extends BaseServices<TDrpcDao, TDrpc> {

    @Override
    public ResultInfo query(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String curpage = "",pagesize="",cxmc="";
            if (jsonObject.containsKey("curpage")) {
                curpage = jsonObject.getString("curpage");
            }
            if (jsonObject.containsKey("pagesize")) {
                pagesize = jsonObject.getString("pagesize");
            }
            if (jsonObject.containsKey("cxmc")) {
                cxmc = jsonObject.getString("cxmc");
            }


            if (StringUtils.isEmpty(curpage)) curpage = "1";
            if (StringUtils.isEmpty(pagesize)) pagesize = "10";

            QueryWrapper<TDrpc> queryWrapper = new QueryWrapper<>();
            Page<TDrpc> page = new Page<>( Integer.parseInt(curpage),  Integer.parseInt(pagesize));

            ListInfo<TDrpc> tDrpcListInfo = new ListInfo<>();
            if (!StringUtils.isEmpty(cxmc))
            {
                queryWrapper.like("ywms",cxmc).or().like("ywjm",cxmc);
            }

            queryWrapper.orderByDesc("id");
            IPage<TDrpc> tDrpcIPage = getBaseMapper().selectPage(page,queryWrapper);

            PageInfo pageInfo = new PageInfo();
            pageInfo.setTotal(tDrpcIPage.getTotal());
            pageInfo.setCurPage(tDrpcIPage.getCurrent());
            pageInfo.setPageSize(tDrpcIPage.getSize());
            pageInfo.setPageTotal(tDrpcIPage.getPages());

            tDrpcListInfo.setPageInfo(pageInfo);
            tDrpcListInfo.setListInfo(tDrpcIPage.getRecords());
            tDrpcListInfo.setUserId(loginId);

            //查询条件的值
            Map<String,Object> cxtjMap = new HashMap<>();
            cxtjMap.put("cxmc",cxmc);

            //返回前端的内容
            Map<String,Object> rtnMap = new HashMap<>();
            rtnMap.put("cxjg",tDrpcListInfo);
            rtnMap.put("cxtj",cxtjMap);

            ResultInfo resultInfo = new ResultInfo(0, "成功返回！",rtnMap);
            return backRtn(resultInfo,loginId);
        }
    }

    @Override
    @Transactional
    public ResultInfo del(String loginId, Map<String, Object> map) {
        JSONObject jsonObject = getData(map,loginId);
        if (StringUtils.isEmpty(loginId)){
            return new ResultInfo(500,"用户信息丢失！",null);
        }else{
            String ids = "";
            if (jsonObject.containsKey("ids")) {
                ids = jsonObject.getString("ids");
            }
            String [] code = ids.split(",");
            int i=0;
            String str="";

            TCzy tCzy = gettCzyDao().getCzyById(loginId);

            for(i=0;i<code.length;i++)
            {
                if (code[i].equals("")) continue;

                String id = code[i];

                TDrpc tDrpc = getBaseMapper().selectById(id);
                if (tDrpc!=null){
                    if (!tDrpc.getCzyId().equals(tCzy.getId())){
                        str+="导入批次["+id+"]非本人导入，禁止删除！";
                    }
                    ToolBean.info("准备删除，这里是异步的");
                    drpcDeleteYwsj(tDrpc,tCzy);
                }
            }
            if (str.length()>0) str = str.substring(0,str.length()-1);
            if (!StringUtils.isEmpty(str)){
                str="["+str+"]在其它位置有记录，无法删除！";
                return new ResultInfo(3,str,null);
            }
            else{
                return new ResultInfo(3,"已执行删除！如果数据量大，可能会有延时，可以稍后查看数据是否完全删除！",null);
            }
        }
    }

    @Async
    @Transactional
    public void drpcDeleteYwsj(TDrpc tDrpc,TCzy tCzy) {
        //导入批次表
        String ywms = "";
        String id = String.valueOf(tDrpc.getId());
        ToolBean.info("准备删除导入批次["+tDrpc.getId()+"]");
        ywms = getBaseMapper().getYwms(id);
        ToolBean.info("准备删除导入批次【"+id+"】，对应的业务表名["+ywms+"]");
        if ("GJSY_SRMX_GX".equals(ywms)){
            //批量更新？放弃，可能性太多，还不如直接重新核算！
        }
        int sl = getBaseMapper().delYwmx(ywms,id);
        ToolBean.info("准备删除导入批次【"+id+"】，对应的业务表名["+ywms+"]，删除数量["+sl+"]");
        tDrpc.setState("0");
        tDrpc.setScbz("准备删除导入批次【"+id+"】，对应的业务表名["+ywms+"]，删除数量["+sl+"]");
        tDrpc.setScry(tCzy.getId());
        tDrpc.setScrq(ToolBean.getToday());
        getBaseMapper().updateById(tDrpc);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public TDrpc getDrpc(String tbname,String ywjm,TCzy tCzy) {
        //导入批次表
        TDrpc tDrpc = new TDrpc();
        tDrpc.setXzjgId(tCzy.getXzjgId());
        tDrpc.setCzyId(tCzy.getId());
        tDrpc.setDrrq(ToolBean.getToday());
        tDrpc.setYwms(tbname);
        tDrpc.setYwjm(ywjm);
        tDrpc.setDrzt("2");
        getBaseMapper().insert(tDrpc);
        return tDrpc;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateDrpc(TDrpc tDrpc) {
        tDrpc.setDrwcsj(ToolBean.getToday());
        getBaseMapper().updateById(tDrpc);
    }
}
